//
//  GJGCChatFriendBurnAfterReadCell.m
//  LEVE
//
//  Created by 上官惠阳 on 2017/3/20.
//  Copyright © 2017年 dashuju. All rights reserved.
//  阅后即焚

#import "GJGCChatFriendBurnAfterReadCell.h"

@interface GJGCChatFriendBurnAfterReadCell ()
@property (nonatomic,assign)CGFloat contentInnerMargin;
@end

@implementation GJGCChatFriendBurnAfterReadCell
{
    GJGCChatFriendContentModel *_chatModel;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        self.contentInnerMargin = 11.f;

        self.typeImgView = [[UIImageView alloc]init];
        self.typeImgView.gjcf_size = (CGSize){36,36};
        self.typeImgView.layer.masksToBounds = YES;
        self.typeImgView.layer.cornerRadius = 18;
        self.typeImgView.userInteractionEnabled = YES;
        [self.bubbleBackImageView addSubview:self.typeImgView];

        self.stateLabel = [[UILabel alloc]init];
        self.stateLabel.font = [UIFont systemFontOfSize:14];
        [self.bubbleBackImageView addSubview:self.stateLabel];

        self.watchTimeLabel = [[UILabel alloc]init];
        self.watchTimeLabel.font = [UIFont systemFontOfSize:12];
        [self.bubbleBackImageView addSubview:self.watchTimeLabel];

        UITapGestureRecognizer *tapR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapOnSelf)];
        tapR.numberOfTapsRequired = 1;
        [self.bubbleBackImageView addGestureRecognizer:tapR];
    }
    return self;
}

- (void)setContentModel:(GJGCChatContentBaseModel *)contentModel
{
    GJGCChatFriendContentModel *chatModel = (GJGCChatFriendContentModel *)contentModel;

    [super setContentModel:chatModel];

    _chatModel = chatModel;
    if(chatModel.isFromSelf){
        self.stateLabel.textColor = [UIColor whiteColor];
        self.watchTimeLabel.textColor = GJCFQuickRGBColor(197,205,255);
    }else{
        self.stateLabel.textColor = [UIColor blackColor];
        self.watchTimeLabel.textColor = GJCFQuickRGBColor(177,183,201);
    }

    self.stateLabel.gjcf_width = 178 - 2*self.contentInnerMargin - self.typeImgView.gjcf_width;
    self.stateLabel.gjcf_height = 17.f;

    self.watchTimeLabel.gjcf_width = 178 - 2*self.contentInnerMargin - self.typeImgView.gjcf_width;
    self.watchTimeLabel.text = chatModel.watchTime;
    self.watchTimeLabel.gjcf_height = 15.f;

    self.bubbleBackImageView.gjcf_height = 58;
    self.bubbleBackImageView.gjcf_width = 178;

    self.typeImgView.gjcf_left = self.contentBordMargin;
    self.typeImgView.gjcf_centerY = self.bubbleBackImageView.centerY;

    self.stateLabel.gjcf_left = self.typeImgView.gjcf_right + self.contentBordMargin-5;
    self.stateLabel.gjcf_top = self.typeImgView.gjcf_top + 3.f;

    self.watchTimeLabel.gjcf_left = self.typeImgView.gjcf_right + self.contentBordMargin-5;
    self.watchTimeLabel.gjcf_top = self.stateLabel.gjcf_bottom + 3;

    [self adjustContent];


    [self loadState];
}

#pragma mark - 长按事件继承

- (void)goToShowLongPressMenu:(UILongPressGestureRecognizer *)sender
{
    [super goToShowLongPressMenu:sender];

    UIMenuController *popMenu = [UIMenuController sharedMenuController];
    if (popMenu.isMenuVisible) {
        return;
    }

    UIMenuItem *item2 = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(deleteMessage:)];
    NSArray *menuItems = @[item2];
    [popMenu setMenuItems:menuItems];
    [popMenu setArrowDirection:UIMenuControllerArrowDown];

    [popMenu setTargetRect:self.bubbleBackImageView.frame inView:self];
    [popMenu setMenuVisible:YES animated:YES];
}

- (void)tapOnSelf
{
    if (_chatModel.readNumber == 2) {
        //不可再点击了
        return;
    }
    if (self.delegate) {
        if (_chatModel.contentType == GJGCChatFriendContentTypeVideoBurnAfterRead &&  [self.delegate respondsToSelector:@selector(videoBurnAfterReadMessageCellDidTap:)]) {

            _chatModel.readNumber += 1;
            [self loadState];

            [self.delegate videoBurnAfterReadMessageCellDidTap:self];
        }else if([self.delegate respondsToSelector:@selector(photoBurnAfterReadMessageCellDidTap:)]){

            _chatModel.readNumber += 1;
            [self loadState];

            [self.delegate photoBurnAfterReadMessageCellDidTap:self];
        }
    }
}
- (void)loadState
{
    if(_chatModel.contentType == GJGCChatFriendContentTypeVideoBurnAfterRead){
        if(_chatModel.readNumber == 2){
            self.typeImgView.image = [UIImage imageNamed:@"im_chat_video_play_prohibit"];
        }else{
            if (_chatModel.isFromSelf) {
                self.typeImgView.image = [UIImage imageNamed:@"im_chat_video_play_white"];
            }else{
                self.typeImgView.image = [UIImage imageNamed:@"im_chat_video_play"];
            }
        }
    }else{
        if(_chatModel.readNumber == 2){
            self.typeImgView.image = [UIImage imageNamed:@"im_chat_picture_play_prohibit"];
        }else{
            if (_chatModel.isFromSelf) {
                self.typeImgView.image = [UIImage imageNamed:@"im_chat_picture_play_white"];
            }else{
                self.typeImgView.image = [UIImage imageNamed:@"im_chat_picture_play"];
            }
        }
    }

    NSString *stateStr;
    if(_chatModel.contentType == GJGCChatFriendContentTypeVideoBurnAfterRead){
        if(_chatModel.readNumber == 2){
            stateStr = @"视频不能再被查看";
        }else if(_chatModel.readNumber == 1){
            stateStr = @"视频还可被查看";
        }else{
            stateStr = @"视频还未被查看";
        }
    }else{
        if(_chatModel.readNumber == 2){
            stateStr = @"图片不能再被查看";
        }else if(_chatModel.readNumber == 1){
            stateStr = @"图片还可被查看";
        }else{
            stateStr = @"图片还未被查看";
        }
    }
    self.stateLabel.text = stateStr;
}

@end
